
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class Camera extends Canvas implements CommandListener, Runnable {

    private Player player;
    private VideoControl videoControl;
    private boolean isSupport = true;
    private Command cmdCapture;
    private Command cmdBack;
    private Image image = null;
    private UpAnh upAnh;

    public Camera(UpAnh upAnh) {
        this.upAnh = upAnh;
        if (!CheckCameraSupport() || !CheckPngEncodingSupport()) {
            isSupport = false;
        }
        if (isSupport) {
            try {
                CreateCamera();
            } catch (Exception ex) {
                ex.printStackTrace();
                isSupport = false;
            }
        }
    }

    private void CreateCamera() throws Exception {
        player = Manager.createPlayer("capture://image");
        player.realize();
        player.prefetch();
        videoControl = (VideoControl) player.getControl("VideoControl");
        videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
        videoControl.setDisplayFullScreen(true);
        videoControl.setDisplayLocation(0, 0);
        videoControl.setDisplaySize(getWidth(), getHeight());
        videoControl.setVisible(true);
        player.start();
        cmdCapture = new Command("Capture", Command.OK, 2);
        cmdBack = new Command("Back", Command.BACK, 2);
        addCommand(cmdCapture);
        addCommand(cmdBack);
        setCommandListener(this);
    }

    private boolean CheckCameraSupport() {
        String propValue = System.getProperty("supports.video.capture");
        return (propValue != null) && (propValue.toLowerCase().equals("true"));
    }

    private boolean CheckPngEncodingSupport() {
        String encodings = System.getProperty("video.snapshot.encodings");
        return (encodings != null) && (encodings.toLowerCase().indexOf("png") != -1);
    }

    protected void paint(Graphics g) {
        if (!isSupport) {
            g.setColor(0);
            g.drawString("Dien thoai ko ho tro chuc nang nay", 0, 0, 0);
            if (image != null) {
                g.drawImage(image, 20, 20, 0);
            }
        }
        System.gc();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdCapture) {
            new Thread(this).start();
        } else if (c == cmdBack) {
            upAnh.DisplayUpAnhChose();
        }
    }

    private boolean Capture() {
        try {
            byte[] images = videoControl.getSnapshot(null);
            upAnh.DisplayUpAnhForm(images, true);
//            upAnh.client.SendImage(images);
            System.gc();
            return true;
        } catch (Exception e) {
            upAnh.DisplayException(e);
        }
        return false;
    }

    public void run() {
        Capture();
    }
}
